package com.hxkcloud.nurse.fragments;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.beans.LoginInfo;
import com.hxkcloud.nurse.dborm.LoginInfoDao;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.utils.SharedPreferenceUtil;
import com.hxkcloud.nurse.views.LoadingDialog;
import com.hxkcloud.nurse.views.MyToast;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by ray_lei on 2016/7/6.
 */
public abstract class BaseFragment extends Fragment {


    protected Activity activity;
    protected Context mContext;
    protected int screenWidth;
    protected int screenHeight;
    protected LoginInfo loginInfo;

    protected LoadingDialog loadingDialog;

    protected ImageLoader imageLoader;
    private boolean viewCreated=false;

    /**布局是否加载成功*/
    public boolean isViewCreated() {
        return viewCreated;
    }

    // 用于刷新UI
    protected Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            dismisssLoading();
            if (msg.what == MessageCode.NETWORKCODE_CONNECT_ERROR) {
                dismisssLoading();
                toastMsg("网络异常，请检测网络");
            }
            updateByHandler(msg);
        }
    };

    public BaseFragment() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        activity = this.getActivity();
        mContext = this.getContext();
        DisplayMetrics dm = new DisplayMetrics();
        this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        loadingDialog = new LoadingDialog(mContext);

        getLoginInfo();

        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public void onResume() {
        super.onResume();
        getLoginInfo();

    }

    protected void getLoginInfo() {
        LoginInfoDao userDao = new LoginInfoDao(mContext);
        loginInfo = null;
        loginInfo = userDao.getUser();
    }

    protected void setTitlePadding(LinearLayout l) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (YjhApplication.StatusBarHeight > 0) {
                l.setPadding(0, YjhApplication.StatusBarHeight, 0, 0);
            } else {
                int height = SharedPreferenceUtil.getStatusBarHeight(getActivity());
                if (height > 0) {
                    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                }
            }
        }
    }

    public void closeInputMethodManager() {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
        if (activity.getCurrentFocus() != null) {
            imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }
        return;
    }

    /**
     * 加载
     */
    public void loading() {
        if (!loadingDialog.isShowing()) {
            loadingDialog.setLoadingText("");
            loadingDialog.show();
        }
    }

    /**
     * 加载
     *
     * @param msg
     */
    public void loading(String msg) {
        if (!loadingDialog.isShowing()) {
            loadingDialog.setLoadingText(msg);
            loadingDialog.show();
        }
    }

    /**
     * 加载
     *
     * @param msg'resource
     */
    public void loading(int msg) {
        if (!loadingDialog.isShowing()) {
            loadingDialog.setLoadingText(msg);
            loadingDialog.show();
        }
    }

    /**
     * 取消加载
     */
    public void dismisssLoading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    /**
     * 提示信息
     *
     * @param msg
     */
    public void toastMsg(String msg) {
        MyToast.geToast().setMsg(msg);
    }

    /**
     * 提示信息
     *
     * @param msg
     */
    public void toastMsg(int msg) {
        MyToast.geToast().setMsg(msg);
    }

    /**
     * toast 图片
     */
    public void toastImg(int id) {
        try {
            Toast toast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);
            LinearLayout layout = (LinearLayout) toast.getView();

            layout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.alpha));
            layout.setOrientation(LinearLayout.HORIZONTAL);
            layout.setGravity(Gravity.CENTER_VERTICAL);
            ImageView iv = new ImageView(mContext);
            iv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            iv.setImageResource(id);
            layout.addView(iv);
            toast.show();
        } catch (Exception e) {
        }
    }

    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        try {
            imageLoader.clearMemoryCache();
        } catch (Exception e) {
        }
        super.onStop();
    }

    protected abstract void initView();

    protected abstract void initListener();

    protected abstract void initData();

    /**
     * 根据handler更新页面数据
     *
     * @param msg
     */
    protected abstract void updateByHandler(Message msg);

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewCreated=true;
    }

}
